/**
* @description: 标准化作业--作业项目
* @author: wqn & yanxiaoyue
* @data:2023年6月17日 - 2023年7月14日
* @remark:
**/
<template>
	<view class="job">
		<nav-bar title="作业项目" @goBack="goBack"></nav-bar>
		<view class="view-details">
			<view class="border">
				<view class="title layout" @click="changeAll">
					<text class="project">{{ title }}</text>
					<van-checkbox v-model="isCheckAll">
						<template #icon="props">
							<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
						</template>
					</van-checkbox>
					<text :class="isCheckAll ? 'active' : ''">全选</text>
				</view>
				<view class="content-list" v-for="(item,index) in jobItemContent" :key="index">
					<view class="distance">
						<view class="affirm layout" @click="handleChecked(item)">
							<img style="width: 22px;height: 22px;"
								src="../../../static/standardizedOperationImg/zxckBlue.png" alt="">
							<text>{{ item.workXm }}</text>
							<van-checkbox shape="square" v-model="item.checked">
								<template #icon="props">
									<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
								</template>
							</van-checkbox>
						</view>
						<van-divider />
						<view class="content">
							<text>
								{{ subTitle }}：<br>
								{{ item.workContent }}
							</text>
						</view>
					</view>
				</view>

			</view>
		</view>
		<view class="verify">
			<van-button type="info" round @click="affirmClick">确认</van-button>
		</view>
	</view>
</template>

<script>
	import navBar from "../common/navBar.vue"
	import ViewDetails from "../common/viewDetails.vue";
	import {
		Dialog,
		Toast
	} from "vant";

	export default {
		name: "jobItem",
		components: {
			ViewDetails,
			navBar
		},
		data() {
			return {
				title: '作业项目',
				subTitle: '作业内容',
				jobItemContent: [],
				isCheckAll: false, // 是否全选
				allCheck: 0,
				activeIcon: require('../../../static/img/no-click.png'),
				inactiveIcon: require('../../../static/img/clicked.png'),
			}
		},
		onLoad(option) {
			this.work = JSON.parse(option.message)
			this.getOoperatingitem();
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			/**
			 * 通知上一个页面刷新数据
			 */
			back() {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.isRefresh = true
				uni.navigateBack()
			},
			// 所有作业项目数据
			getOoperatingitem() {
				this.$apiRequest.bzhzyApi.selectoperatingitem({
					consultationDeductionId: this.work.OBJID
				}).then(res => {
					res.data.forEach(function(item) {
						'operatingItemId' in item ? item.checked = true : item.checked = false
					})
					this.jobItemContent = res.data;
					let CheckedAllArr = [];
					CheckedAllArr = this.jobItemContent.filter(item => item.checked == true);
					this.allCheck = CheckedAllArr.length;
					this.allCheck === this.jobItemContent.length ? this.isCheckAll = true : this.isCheckAll =
						false;
					console.log(this.jobItemContent, '修改后的数据');
				})
			},
			//全选
			changeAll() {
				this.isCheckAll = !this.isCheckAll;
				this.isCheckAll ? this.jobItemContent.forEach(function(item) {
					item.checked = true;
				}) : this.jobItemContent.forEach(function(item) {
					item.checked = false;
				})
			},
			//单选
			handleChecked(item) {
				let CheckedAllArr = [];
				CheckedAllArr = this.jobItemContent.filter(item => item.checked == true);
				this.allCheck = CheckedAllArr.length;
				item.checked = !item.checked;
				item.checked ? this.allCheck++ : this.allCheck--
				this.allCheck === this.jobItemContent.length ? this.isCheckAll = true : this.isCheckAll = false;
			},
			//确认提交
			affirmClick() {
				let CheckedAllArr = [];
				this.jobItemContent.filter(function(item) {
					if (item.checked === true) {
						CheckedAllArr.push(item.objId)
					}
				});
				Dialog.confirm({
						message: '提交后不可修改，是否确定提交？',
						confirmButtonColor: "#417adc",
					})
					.then(() => {
						// on confirm
						this.$apiRequest.bzhzyApi.insertoperatingitemlog({
							workPlanId: this.work.id,
							consultationDeductionId: this.work.OBJID,
							tFlowContentId: this.work.flowId,
							array: CheckedAllArr
						}).then(res => {
							Toast(res.msg)
							this.back();
						}).catch(error => {
							Toast("提交失败")
						})
					})
					.catch(() => {
						// on cancel
					});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.job {
		display: flex;
		flex-direction: column;
		overflow: hidden;
		width: 100vw;
		height: 100vh;
		background: #F2F6FA;
	}

	.verify {
		width: 100%;
		padding: px2vh(40);
		box-sizing: border-box;

		.van-button--normal {
			width: 100%;
			height: px2vh(120);
			font-size: px2vh(46);
			background: #417ADC;
			border: none;
		}
	}

	.view-details {
		flex: 1;
		width: 100vw;
		overflow: scroll;

		.layout {
			display: flex;
			align-items: center;
		}

		text {
			font-size: px2vh(40);
			color: #666666;
			font-weight: bold;
		}

		.border {
			padding: px2vh(50) px2vw(40) px2vh(20);
		}

		.title {
			margin-bottom: px2vh(30);

			.project {
				flex: 1;
			}

			text:last-child {
				color: #98999B;
				margin-left: px2vw(10);
			}
		}

		.content-list {
			margin-bottom: px2vh(30);
			background: #FFFFFF;
			border-radius: px2vh(20);

			.distance {
				padding: px2vh(52) px2vw(50) px2vh(40);
			}

			.affirm {
				text {
					margin-right: auto;
					margin-left: px2vw(18);
				}
			}

			.content {
				width: px2vw(920);
				background: #F7F7F7;

				text {
					display: block;
					padding: px2vh(48);
					font-weight: normal;
					font-size: px2vh(38);
					line-height: px2vh(62);
				}
			}
		}
	}

	.img-icon {
		width: 13px;
		height: 13px;
	}

	.active {
		color: #417ADC !important;
	}
</style>
